package com.zhongjianjian.leguansuo.service;

import com.alibaba.fastjson.JSON;
import com.zhongjianjian.leguansuo.dao.TestLgDao;
import com.zhongjianjian.redisLock.entity.TestStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 作者: LCG
 * 日期: 2020/2/22 00:18
 * 描述:
 */
@Component
public class TestLgService {

    @Autowired
    private TestLgDao testLgDao;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public void testStock(TestStock testStock){

        TestStock stock = testLgDao.getStock(testStock);

        stock.setStock(stock.getStock()-1);
        int updateStock = testLgDao.updateStock(stock);
        System.out.println(updateStock+"========更新库存======"+stock.getStock());

        if(updateStock==0){
            //放入Redis队列
            String toJSONString = JSON.toJSONString(testStock);
            redisTemplate.convertAndSend("LG",toJSONString);
        }
    }
}
